package com.right.health.app.net;

import android.text.TextUtils;

import com.magics.http.model.HttpParams;
import com.right.health.app.app.App;
import com.right.health.app.bean.FamilyResponse;
import com.right.health.app.bean.OrderResponse;
import com.right.health.app.bean.PayClinicListResponse;
import com.right.health.app.bean.PayInfo;
import com.right.health.app.ui.registered.PayClinicOrderActivity;

import org.json.JSONException;
import org.json.JSONObject;

/*
 * Created by hopeliao on 2019-12-22
 *
 */public class OrderApi extends IBaseApi {

     public void addOrder(String personid, String memberid, String scheduleid, int ordertype) {
         HttpParams params = new HttpParams();
         try {
             JSONObject jsonObject = new JSONObject();
             jsonObject.put("personid", personid);
             jsonObject.put("memberid", memberid);
             jsonObject.put("scheduleid", scheduleid);
             jsonObject.put("ordertype", ordertype);

             params.addFormParam("jsonstr", jsonObject.toString());
         } catch (JSONException e) {
             e.printStackTrace();
         }
         doPostJson(HttpConstants.API_ADD_ORDER, params, PayInfo.class);
     }

    public void toTestPay(String personid, String memberid, String scheduleid, int ordertype) {
        HttpParams params = new HttpParams();
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("orderid", "123111423232431f3d4");
            jsonObject.put("totalfee", String.valueOf(1));

            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson("https://doctor.yb-aliyun.com/pandWx/ybwx/wxpay.json", params, PayInfo.class);
    }

    public void getOrder(int pageNo, String userId) {
        HttpParams params = new HttpParams();
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("personid", userId);
            PageUtil.addPageJson(jsonObject, pageNo);
            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_ORDER_LIST, params, OrderResponse.class);
    }

    public void cancelOrder(String orderid) {
        HttpParams params = new HttpParams();
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("orderid", orderid);
            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_CANCEL_ORDER, params, null);
    }

    public void toPay(String orderid, String type) {
        HttpParams params = new HttpParams();
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("orderid", orderid);
            jsonObject.put("ordertype", type);
            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_PAY_ORDER, params, PayInfo.class);
    }


    public void getPayClinicList(String type, String id,int page) {
        HttpParams params = new HttpParams();
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("memberid", id);
            PageUtil.addPageJson(jsonObject, page);
            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (TextUtils.equals(type, "unPay")) {
            doPostJson(HttpConstants.API_PAY_CLINIC_UN_PAY, params, PayClinicListResponse.class);
        } else if (TextUtils.equals(type, "paid")) {
            doPostJson(HttpConstants.API_PAY_CLINIC_PAID, params, PayClinicListResponse.class);
        }
    }

    public void getPatientList(){
        HttpParams params = new HttpParams();
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("personid", App.getInstance().getUserId());

            PageUtil.addPageJson(jsonObject, 1, 9999);

            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_PATIENT_LIST, params, FamilyResponse.class);
    }

    /**
     * 诊间支付订单待支付
     */
    public void getPayClinicOrder(String id){
        HttpParams params = new HttpParams();
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("prescriptionid", id);
            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_PAY_CLINIC_ORDER, params, null);
    }

    /**
     * 诊间支付订单已支付详情
     */
    public void getPayClinicDetail(String id){
        HttpParams params = new HttpParams();
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("prescriptionid", id);
            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_PAY_CLINIC_DETAIL, params, null);
    }

    /**
     * 诊间支付
     */
    public void payClinic(String id,String password,String type){
        HttpParams params = new HttpParams();
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("prescriptionid", id);
            jsonObject.put("ordertype",type);
            if (TextUtils.equals(PayClinicOrderActivity.PAY_TYPE_SOCIAL+"","12")) {
                jsonObject.put("paypwd",password);
            }
            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_PAY_CLINIC, params, PayInfo.class);
    }

    /**
     * 获取医保APP支付加密参数
     */
    public void getSocialParam(String id,String mbType){
        HttpParams params = new HttpParams();
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("orderid", id);
            jsonObject.put("channel_id", "O10004");
            jsonObject.put("akb020","45096068251040211A3001");
            jsonObject.put("yka316",mbType);
            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_GET_SOCIAL_PAY_PARAM, params, null);
    }
}
